﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LitJson;
using UnityEngine.UI;
using System.IO;

public class addcardtodeck : MonoBehaviour {

    [SerializeField]
    InputField input;
    public JsonData cardtoadd;
    public List<cardtosave1> cardtoadd1 = new List<cardtosave1>();
    private cardcontrol cardinfo;
    GameObject loaddecktext;
    GameObject loaddeckinput;
    GameObject loaddeck11;
    GameObject startimg;
    GameObject loaddeckbtn1;

    private void Awake()
    {
        loaddecktext = GameObject.Find("loaddecktext");
        loaddeckinput = GameObject.Find("loaddeckinput");
        loaddeck11 = GameObject.Find("loaddeck11");
        startimg = GameObject.Find("startimg");
        loaddeckbtn1 = GameObject.Find("loaddeck");
    }

    void cardtoadd1con()
    {
        for (int i = 0; i < cardtoadd.Count; i++)
        {
            cardtoadd1.Add(new cardtosave1(cardtoadd[i]["name"].ToString(), (int)cardtoadd[i]["amount"]));
        }
    }

    void cardtoaddadd()
    {
        for (int i = 0; i < cardtoadd1.Count; i++)
        {
            cardinfo.AddCard(cardtoadd1[i].name);
            Debug.Log("1");
            carddata savedcardamount1 = cardinfo.slots[i].transform.GetChild(0).GetComponent<carddata>();
            savedcardamount1.amount = cardtoadd1[i].amount;
            cardinfo.slots[i].transform.GetChild(0).transform.GetChild(0).GetComponent<Text>().text = savedcardamount1.amount.ToString();
         }
    }


    void Start () {
        cardinfo = GameObject.Find("cardmanager").GetComponent<cardcontrol>();
    }
	
	public void click()
    {
        string path = "/cards/" + input.text + ".json";
        cardtoadd = JsonMapper.ToObject(File.ReadAllText(Application.dataPath + path));
        cardtoadd1con();
        cardtoaddadd();
        loaddecktext.SetActive(false);
        loaddeckinput.SetActive(false);
        loaddeck11.SetActive(false);
        startimg.SetActive(false);
        loaddeckbtn1.SetActive(false);
    }
}
